home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1042 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  55 lines

  1. Path: newsbf02.news.aol.com!not-for-mail
  2. From: residue@aol.com (Residue)
  3. Newsgroups: comp.lang.c++
  4. Subject: [Q] Friends of nested classes
  5. Date: 8 Jan 1996 19:40:39 -0500
  6. Organization: America Online, Inc. (1-800-827-6364)
  7. Sender: root@newsbf02.news.aol.com
  8. Message-ID: <4csdi7$afa@newsbf02.news.aol.com>
  9. Reply-To: residue@aol.com (Residue)
  10. NNTP-Posting-Host: newsbf02.mail.aol.com
  11.  
  12. How does one define a friend function of a nested class, taking an operand
  13. of that class, outside the class declaration? For example, this code
  14. compiles just fine (with MS Visual C++ 4.0, at least):
  15.  
  16.     #include <Foo.h>  //  class Foo { };
  17.     #include <Bar.h>  //  class Bar { class Imp; };
  18.  
  19.     class Bar::Imp
  20.     {
  21.         friend Foo& operator+=(Foo& f, Imp i) { return f; }  //  OK
  22.     };
  23.  
  24. but when I move the function definition outside the declaration, it fails:
  25.  
  26.     #include <Foo.h>  //  class Foo { };
  27.     #include <Bar.h>  //  class Bar { class Imp; };
  28.  
  29.     class Bar::Imp
  30.     {
  31.         friend Foo& operator+=(Foo& f, Imp i);
  32.     };
  33.  
  34.     Foo& operator+=(Foo& f, Bar::Imp i) { return f; }  //  wrong(?)
  35.  
  36. Should it? I don't want to change the header files, so I'm using helper
  37. member functions as a workaround at the moment, as in:
  38.  
  39.     #include <Foo.h>  //  class Foo { };
  40.     #include <Bar.h>  //  class Bar { class Imp; };
  41.  
  42.     class Bar::Imp
  43.     {
  44.         static Foo& h(Foo& f, Imp i);
  45.         friend Foo& operator+=(Foo& f, Imp i) { return h(f, i); }
  46.     };
  47.  
  48.     Foo& Bar::Imp::h(Foo& f, Bar::Imp i) { return f; }  //  OK
  49.  
  50. but I'd rather not if I don't have to. What's the RIGHT way to do this?
  51.  
  52.  
  53. Bryan Bowe
  54. residue@aol.com
  55.